<template>
	<view class="" :class="ggclass">
		<uni-popup ref="popRef"  @close="close" :safe-area="false">
			<view class="picker-view-box">
				<view class="picker-title">
					<text class="" @click="close">取消</text>
					<view class="font_s32 font-weight">{{title}}</view>
					<text class="blue" @click="submit">确定 </text>
				</view>
				<picker-view v-if="visible" :indicator-style="indicatorStyle" :value="values" @change="bindChange"
					class="picker-view">
					<picker-view-column>
						<view class="item" v-for="(item,index) in years" :key="index">{{item}}年</view>
					</picker-view-column>
					<picker-view-column>
						<view class="item" v-for="(item,index) in months" :key="index">{{item}}月</view>
					</picker-view-column>
					<picker-view-column>
						<view class="item" v-for="(item,index) in days" :key="index">{{item}}日</view>
					</picker-view-column>
				</picker-view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		dateFilter as filter
	} from '../ways/filter.js'
	const date = new Date()
	export default {
		name:'c-picker-view-date',
		props: {
			title: {
				type: String,
				default: '自定义'
			},
			disabled: {
				type: Boolean,
				default: false
			},
			value: {
				type: Array,
				default: () => [2022, date.getMonth(), date.getDate() - 1]
			},
			range_key: {
				type: String,
				default: 'name'
			},
			range_value: {
				type: String,
				default: 'id'
			},
			immediate: {
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				years: [],
				year: date.getFullYear(),
				months: [],
				month: date.getMonth() +1,
				days: [],
				day: date.getDate(),
				values: [],
				visible: false,
				indicatorStyle: `height: 50px;`,
				params:{}
			}
		},
		mounted() {
			for (let i = 1990; i <= date.getFullYear(); i++) {
				this.years.push(i)
			}
			this.months = this.getMonths(this.year)
			this.days = this.getDate(this.year, this.month)
		},
		methods: {
			getMonths(year) {
				let months = []
				let num = 0
				if (year == this.year) {
					num = date.getMonth() + 1
				} else {
					num = 12
				}
				for (let i = 1; i <= num; i++) {
					months.push(i)
				}
				return months
			},
			getDate(year, month) {
				let days = []
				let num = 0
				if (year == this.year && month == this.month) {
					num = date.getDate()
				} else {
					num = new Date(year, month, 0).getDate()
				}
				for (let i = 1; i <= num; i++) {
					days.push(i)
				}
				return days
			},
			open() {
				if (this.disabled) return
				this.$refs.popRef.open('bottom')
				this.visible = true
				this.setValues()
			},
			close(){
				this.$refs.popRef.close()
				this.visible = false
			},
			setValues() {
				let defaultValues = filter(date).split('-')
				console.log(date,'date')
				this.values = [
					this.years.findIndex(v => v == defaultValues[0]),
					this.months.findIndex(v => v == defaultValues[1]),
					this.days.findIndex(v => v == defaultValues[2])
				]
				this.bindChange({detail:{value:this.values}})
			},
			bindChange(e) {
				const val = e.detail.value
				// console.log(val,'val')
				let year = this.years.find((v, i) => val[0] == i)
				let month = this.months.find((v, i) => val[1] == i)
				let day = this.days.find((v, i) => val[2] == i)
				this.months = this.getMonths(year)
				this.days = this.getDate(year, month-1)
				let data = [year, month, day].join('-')
				this.params= data
			},
			submit() {
				this.close()
				uni.showLoading()
				let time = setTimeout(() => {
					this.$emit('submit',filter(this.params,'yyyyMMdd'))
					this.$emit('input',filter(this.params,'yyyyMMdd'))
					this.$emit('update:name',filter(this.params,'yyyy年MM月dd日'))
					uni.hideLoading()
					clearTimeout(time)
				}, 1000)
			},
		}
	}
</script>
<style lang="scss" scoped>
	.picker-view-box {
		position: relative;
		z-index: 1000;
		background-color: #fff;
		border-radius: 32rpx 32rpx 0 0;
	}

	.picker-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
	}

	.picker-view {
		width: 750rpx;
		height: 500rpx;
		// margin-top: 20rpx;
	}

	.item {
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		text-align: center;
	}
</style>
